﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace project
{
    public partial class EditBook : UIForm
    {
        private string old_booknumber;
        public EditBook()
        {
            InitializeComponent();
        }
        public EditBook(string ISBN,string  BookName,string  BookAuthor,string  category,string  Publisher,string last_mount,string  Store,string  location)
        {
            InitializeComponent();
            old_booknumber = ISBN;
            uiTextBox1.Text=ISBN;
            uiTextBox2.Text=BookName;
            uiTextBox3.Text=BookAuthor;
            uiTextBox4.Text=category;
            uiTextBox5.Text=Publisher;
            uiTextBox6.Text=Store;
            uiTextBox7.Text=location;
            uiTextBox8.Text=last_mount;
        }
            

        private void uiButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void ClearTextBox()
        {
            uiTextBox1.Clear();
            uiTextBox2.Clear();
            uiTextBox3.Clear();
            uiTextBox4.Clear();
            uiTextBox5.Clear();
            uiTextBox6.Clear();
            uiTextBox7.Clear();
            uiTextBox8.Clear();
        }


        private void uiButton1_Click(object sender, EventArgs e)
        {
            string ISBN = uiTextBox1.Text.Trim();
            string BookName = uiTextBox2.Text.Trim();
            string BookAuthor = uiTextBox3.Text.Trim();
            string category = uiTextBox4.Text.Trim();
            string Publisher = uiTextBox5.Text.Trim();
            string Store = uiTextBox6.Text.Trim();
            string location = uiTextBox7.Text.Trim();
            string last_mount = uiTextBox8.Text.Trim();

            if (string.IsNullOrEmpty(ISBN) ||
                string.IsNullOrEmpty(BookName) ||
                string.IsNullOrEmpty(BookAuthor) ||
                string.IsNullOrEmpty(Publisher) ||
                string.IsNullOrEmpty(category) ||
                string.IsNullOrEmpty(Store) ||
                string.IsNullOrEmpty(location)||
                string.IsNullOrEmpty(last_mount))
            {
                MessageBox.Show("存在空数据，请检查后再添加", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            string sqlStr = $"update books set " +
           $"number='{ISBN}'," +
           $"name='{BookName}'," +
           $"author='{BookAuthor}'," +
           $"category='{category}'," +
           $"publish='{Publisher}'," +
           $"repertory=repertory+({int.Parse(Store)-int.Parse(old_booknumber)})," +
           $"total_mount={int.Parse(Store)}," +
           $"location='{location}' " +
           $"where number='{old_booknumber}';";

            int reslut = SqlHelper.ExecuteNonQuery(sqlStr);
            if (reslut == 1)
            {

                MessageBox.Show("修改图书信息成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.Close();
            }
            else
            {
                MessageBox.Show("修改图书信息失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void uiButton2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
